var express = require('express');
var router = express.Router();
var http=require('http');
var url = require('url');

router.all('*',function(req,res,next){
	res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
})
//轮播的接口  localhost:3000/lunbo?time=1495519120152
router.get('/lunbo',function(req, res){
	var t=req.query.time;
	var url='http://m.maizuo.com/v4/api/billboard/home?__t=' + t;
	http.get(url,function(response){
		var data='';
		response.on('data',function(chunk){
			data+=chunk;
		})
		response.on('end',function(){
			res.send(data);
		})
	})
})

router.get('/nowList',function(req, res){
	var t=req.query.time;
	var url='http://m.maizuo.com/v4/api/film/now-playing?__t='+t+'&page=1&count=5'
	http.get(url,function(response){
		var data='';
		response.on('data',function(chunk){
			data+=chunk;
		})
		response.on('end',function(){
			res.send(data);
		})
	})
})

//即将上映列表
//http://m.maizuo.com/v4/api/film/coming-soon?page=1&count=7
router.get('/filmlist/soonplay',function(req,res){
	var p=req.query.p;
	var url='http://m.maizuo.com/v4/api/film/coming-soon?page='+p+'&count=7'
	http.get(url,function(response){
		var data='';
		response.on('data',function(chunk){
			data+=chunk;
		})
		response.on('end',function(){
			res.send(data);
		})
	})
})

router.get('/soonList',function(req, res){
	var t=req.query.time;
	var url='http://m.maizuo.com/v4/api/film/coming-soon?__t='+t+'&page=1&count=3'
	http.get(url,function(response){
		var data='';
		response.on('data',function(chunk){
			data+=chunk;
		})
		response.on('end',function(){
			res.send(data);
		})
	})
})

//电影列表
//http://m.maizuo.com/v4/api/film/now-playing?page=1&count=7
router.get('/filmlist/nowlist',function(req,res){
	var p=req.query.p;
	var url='http://m.maizuo.com/v4/api/film/now-playing?page='+p+'&count=7';
	http.get(url,function(response){
		var data='';
		response.on('data',function(chunk){
			data+=chunk;
		})
		response.on('end',function(){
			res.send(data);
		})
	})
})

//电影详情
//http://m.maizuo.com/v4/api/film/3664?__t=1495792294942
router.get(/\d$/,function(req,res){
	var t=req.query.time;
	var id=url.parse(req.url).pathname.substr(1);
	var url1='http://m.maizuo.com/v4/api/film/'+id+'?__t='+t
	http.get(url1, function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    });
})

//地区影院列表
//http://m.maizuo.com/v4/api/cinema?__t=1495857742614
router.get('/cinemalist',function(req,res){
	var t=req.query.time;
	var url='http://m.maizuo.com/v4/api/cinema?__t='+t;
	http.get(url,function(response){
		var data='';
		response.on('data',function(chunk){
			data+=chunk;
		})
		response.on('end',function(){
			res.send(data);
		})
	})
})

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;
